package cn.mabach.goods.service.impl;


import cn.mabach.goods.dao.ProductAttributeDao;
import cn.mabach.goods.dto.ProductAttrInfo;
import cn.mabach.goods.entity.ProductAttributeEntity;
import cn.mabach.goods.service.ProductAttributeService;
import cn.mabach.result.PageResult;
import cn.mabach.result.RS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeDao, ProductAttributeEntity> implements ProductAttributeService {

    @Autowired
    private ProductAttributeDao productAttributeDao;
    @Override
    public RS<PageResult<ProductAttributeEntity>> queryPage(@RequestParam("cid") Long cid,
                                                            @RequestParam(value = "type",required = false) Integer type,
                                                            @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize" , defaultValue = "10") Integer pageSize) {
        if (pageSize>20){
            return RS.error("ProductAttributeServiceImpl非法参数");
        }
        IPage<ProductAttributeEntity> page = this.page(
                new Page<ProductAttributeEntity>(pageNum,pageSize),
                new QueryWrapper<ProductAttributeEntity>().eq("product_attribute_category_id",cid)
                .eq(type!=null,"type",type)

        );

        return RS.ok(new PageResult(page));
    }





    @Override
    public RS<ProductAttributeEntity> getByIdE(@RequestParam("id") Long id) {
            ProductAttributeEntity res = this.getById(id);

        return RS.ok(res);
    }

    @Override
    public RS saveE(@RequestBody  ProductAttributeEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return RS.error();
        }
        return RS.ok();
    }

    @Override
    public RS updateByIdE(@RequestBody ProductAttributeEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return RS.error();
        }
        return RS.ok();
    }


    @Override
    public RS removeOneById(@RequestParam("id") Long id) {
        boolean b = this.removeById(id);
        if (!b){
            return RS.error();
        }
        return RS.ok();
    }

    @Override
    @Transactional
    public RS removeByIdsE(@RequestParam("ids") List<Long> ids) {
        boolean b = this.removeByIds(ids);
        if (!b){
            return RS.error();
        }
        return RS.ok();
    }

    @Override
    public RS<List<ProductAttrInfo>> getAttrInfo(@RequestParam("productCategoryId") Long productCategoryId) {
        List<ProductAttrInfo> productAttrInfo = productAttributeDao.getProductAttrInfo(productCategoryId);
        return RS.ok(productAttrInfo);
    }

}